package dao.impl;

import dao.EmployeeDao;
import entity.Employee;
import util.ConnectionFactory;
import util.DateAndLocalDate;
import util.JdbcTemplate;
import util.PreparedStatementCreator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {

    private JdbcTemplate template = new JdbcTemplate();

    @Override
    public Employee findByNameAndGender(String name, int gender) {
        // Connection connection =
        return template.queryOne( (conn) -> {
           String sql = "select * from employ where name = "+name+" and gender = " + gender;
           PreparedStatement preparedStatement = conn.prepareStatement(sql);
           return preparedStatement;
        }, (resultSet) -> {
            List<Employee> employeeList = new ArrayList<>();
            while (resultSet.next())  {
                int ID = resultSet.getInt("ID");
                String Name = resultSet.getString("Name");
                LocalDate birthday = resultSet.getDate("Birthday").toLocalDate();
                int gender_ = resultSet.getInt("Gender");
                String phone = resultSet.getString("Phone");
                employeeList.add(new Employee(ID,Name,birthday,gender_,phone));
            }
            return employeeList;
        });
    }

    @Override
    public List<Employee> findAll() {
        return template.queryList( (connection) -> {
            String sql = "select * from employ";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            return pstmt;
        }, (resultSet) -> {
            List<Employee> employeeList = new ArrayList<>();
            while (resultSet.next())  {
                int ID = resultSet.getInt("ID");
                String Name = resultSet.getString("Name");
                LocalDate birthday = resultSet.getDate("Birthday").toLocalDate();
                int gender = resultSet.getInt("Gender");
                String phone = resultSet.getString("Phone");
                employeeList.add(new Employee(ID,Name,birthday,gender,phone));
            }
            return employeeList;
        });
    }

    @Override
    public void addEmployee(Employee employee) {
        template.update((connection) -> {
            String sql = "insert into employ(ID,Name,Birthday,Gender,Phone) values (?,?,?,?,?)";
            // String sql = "insert into employ(Name,Birthday,Gender,Phone) values (?,?,?,?)";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            //绑定参数
            pstmt.setObject(1,employee.getId());
            pstmt.setObject(2,employee.getName());
            pstmt.setObject(3,new Date(DateAndLocalDate.localDateToDate(employee.getBirthday()).getTime()));
            pstmt.setObject(4,employee.getGender());
            pstmt.setObject(5,employee.getPhone());
            return pstmt;
        });
    }

    @Override
    public void deleteEmployee(Employee employee) {
        String sql = "delete from employ "+"where ID = "+employee.getId();
        try (Connection conn = ConnectionFactory.getConnection()) {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateEmployee(Employee employee) {
        String sql = "update employ set Name = ?,Birthday = ?,Gender = ?,Phone = ? where ID = ?";
        try (Connection conn = ConnectionFactory.getConnection()) {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setObject(1,employee.getName());
            preparedStatement.setObject(2,new Date(DateAndLocalDate.localDateToDate(employee.getBirthday()).getTime()));
            preparedStatement.setObject(3,employee.getGender());
            preparedStatement.setObject(4,employee.getPhone());
            preparedStatement.setObject(5,employee.getId());
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
